<?php
class Link_Manager extends Model {
	private $_current_blogger_id;
	
	function Link_Manager() {
		parent::Model();
		$this->_current_blogger_id = 0;
	}
	
	function SetCurrentBloggerID($current_blogger_id) {
		$this->_current_blogger_id = $current_blogger_id;
	}
	
	function _check_blogger_id() {
		if($this->_current_blogger_id == 0) {
			throw new Exception('ERROR: Invalid Blogger-ID!');
		}
	}
	
	function AddLink($link) {
		$this->_check_blogger_id();
		
		$parms = array(
			'title' => $link['title'],
			'url' => $link['url'],
			'blogger_id' => $this->_current_blogger_id,
			'description' => $link['description']
		);
		$this->db->insert('nt_links', $parms);
	}
	
	function UpdateLink($link) {
		$this->_check_blogger_id();
		
		$parms = array(
			'title' => $link['title'],
			'url' => $link['url'],
			'description' => $link['description']
		);
		$this->db->where(array('id' => $link['id'], 'blogger_id' => $this->_current_blogger_id));
		$this->db->update('nt_links', $parms);
	}
	
	function RemoveLink($link_id) {
		$this->_check_blogger_id();
		
		$this->db->query('DELETE FROM `nt_links` WHERE `id` = ? AND `blogger_id` = ?', array($link_id, $this->_current_blogger_id));
	}
	
	function GetLinkInfo($link_id) {
		$qry = $this->db->query('SELECT * FROM `nt_links` WHERE `id` = ? AND `blogger_id` = ?', array($link_id, $this->_current_blogger_id));
		return $qry->row();
	}
}
?>